<?php

class FRM_Controller_Plugin_Standart2 extends Zend_Controller_Plugin_Abstract {
	private $myFrontController;
	private $myView;
	private $myModule;
	
	public function routeStartup($request) {
		/**
		 * Добавляем пути к контроллерам
		 */
		$this->myFrontController = Zend_Controller_Front::getInstance ();
		
		$dir = dir ( CONTROLLERS );
		while ( false !== ($module = $dir->read ()) ) {
			if ($module != '.' && $module != '..') {
				if (is_dir ( CONTROLLERS . $module )) {
					$this->myFrontController->addControllerDirectory ( CONTROLLERS . $module . '/', $module );
				}
			}
		}
	}
	
	public function dispatchLoopStartup($request) {
		$this->myFrontController->setDefaultModule ( $request->getModuleName () );
	}
	
	public function preDispatch($request) {
		$this->myFrontController = Zend_Controller_Front::getInstance ();
		$smarty = new FRM_View_Smarty ( BASE_VIEW . $request->getModuleName () . '/' );
		
		Zend_Registry::set ( 'Zend_View', $this->myView = $smarty );
		/**
		 * Настройка плагина viewRenderer
		 */
		Zend_Controller_Action_HelperBroker::getStaticHelper ( 'viewRenderer' )->setView ( $this->myView )->setViewSuffix ( 'tpl' );
		
		FRM_ACL::$currentModule = $request->getModuleName ();
		
		if ($request->getModuleName () === 'admin') {
			
			FRM_Controller_Action_Abstract::$currentAuth = FRM_Auth_Admin::getInstance ();
			FRM_Db_Table_Abstract::$currentAuth = FRM_Auth_Admin::getInstance ();
			FRM_ACL::$currentAuth = FRM_Auth_Admin::getInstance ();
			
			if (! FRM_ACL::hasPermition ( $request->getControllerName (), $request->getActionName () )) {
				$request->setControllerName ( 'error' );
				$request->setActionName ( 'access' );
			} else {
				$a = new Admin_Auth ( );
				$a->updateLastActivity ();
			}
		}
		
		if ($request->getModuleName () === 'operator') {
			
			FRM_Controller_Action_Abstract::$currentAuth = FRM_Auth_Operator::getInstance ();
			FRM_Db_Table_Abstract::$currentAuth = FRM_Auth_Operator::getInstance ();
			FRM_ACL::$currentAuth = FRM_Auth_Operator::getInstance ();
			
			if (! FRM_ACL::hasPermition ( $request->getControllerName (), $request->getActionName () )) {
				$request->setControllerName ( 'error' );
				$request->setActionName ( 'access' );
			} else {
				$a = new Admin_Auth ( );
				$a->updateLastActivity ();
			}
		}
		
		if ($request->getModuleName () === 'supervisor') {
			
			FRM_Controller_Action_Abstract::$currentAuth = FRM_Auth_Supervisor::getInstance ();
			FRM_Db_Table_Abstract::$currentAuth = FRM_Auth_Supervisor::getInstance ();
			FRM_ACL::$currentAuth = FRM_Auth_Supervisor::getInstance ();
			
			if (! FRM_ACL::hasPermition ( $request->getControllerName (), $request->getActionName () )) {
				$request->setControllerName ( 'error' );
				$request->setActionName ( 'access' );
			} else {
				$a = new Admin_Auth ( );
				$a->updateLastActivity ();
			}
		}
		
		if ($request->getModuleName () === 'agents') {
			FRM_Controller_Action_Abstract::$currentAuth = FRM_Auth_Agents::getInstance ();
			FRM_Db_Table_Abstract::$currentAuth = FRM_Auth_Agents::getInstance ();
		}
	
	}
	
	/**
	 * Enter description here...
	 *
	 * @param unknown_type $request
	 */
	public function postDispatch($request) {
		
		$this->myView->setBasePath ( BASE_VIEW . $request->getModuleName () . '/' );
	}
}